package com.ecode.ecny.service.sign;

import com.ecode.ecny.exception.TrxErrorEnums;
import com.ecode.ecny.exception.TrxException;
import com.ecode.ecny.base.TxnStatus;
import com.ecode.ecny.dao.sign.SignInfoDtlMapper;
import com.ecode.ecny.dao.sign.SignInfoMapper;
import com.ecode.ecny.mobel.sign.SignInfoDo;
import com.ecode.ecny.mobel.sign.SignInfoDtlDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName SignHelpService
 * @Author mjlft
 * @Date 2021/8/15 18:21
 * @Version 1.0
 * @Description 签约辅助服务
 */
@Service
public class SignHelpService {
    private Logger logger = LoggerFactory.getLogger(SignHelpService.class);

    @Autowired
    private SignInfoMapper signInfoMapper;
    @Autowired
    private SignInfoDtlMapper signInfoDtlMapper;

    @Transactional(rollbackFor = Exception.class)
    public void signInfo(boolean insert, SignInfoDo signInfoDo, SignInfoDtlDO signInfoDtlDO) {
        int res = 0;
        int resDtl = 0;
        try {
            if (insert) {
                res = signInfoMapper.sign(signInfoDo);
            } else {
                res = signInfoMapper.updateStatusBySignNo(signInfoDo);
            }
            resDtl = signInfoDtlMapper.updateSignNo(signInfoDtlDO);
            if (res != 1 || resDtl != 1) {
                throw new TrxException(TrxErrorEnums.SYS_ERROR, TxnStatus.FAIL);
            }
        }catch (TrxException e){
            logger.info("【签约】，协议入库失败");
            throw e;
        }catch (Exception ex){
            logger.info("【签约】，协议入库失败", ex);
            throw new TrxException(TrxErrorEnums.SYS_ERROR, TxnStatus.FAIL);
        }
    }
}
